// +build ignore

#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>

#include <linux/myskbuff.h>
// Simple map of IP address to counts
struct
{
	__uint(type, BPF_MAP_TYPE_HASH);
	__type(key, __u32);
	__type(value, __u32);
	__uint(max_entries, 10);
} pings_map SEC(".maps");


SEC("kprobe/icmp_echo")
int dump_pings(struct pt_regs *ctx)
{
         struct __sk_buff skb;
         struct sk_buff* ptr;
         struct iphdr* ip_header;
         __u32 addr;
         __u32 *p;
         __u32 counter = 0;

         bpf_probe_read(&skb, sizeof(skb), (const void*)((ctx)->rdi));

         ptr = (struct sk_buff*)&skb;
         addr = skb.remote_ip4;

         char buf[] = "XXX Debugging inside our BPF! skb.len: %x  addr: %x  ptr: %x\n";
         bpf_trace_printk(buf, sizeof(buf), skb.len, addr, ptr);

         p = bpf_map_lookup_elem(&pings_map, &addr);
         if( p != 0) {
                 counter = *p;
         }

         counter  ++;
         bpf_map_update_elem(&pings_map, &addr, &counter, 0);

         return 0;
}

char _license[] SEC("license") = "GPL";
